<template>
  <page>
    <template v-slot:header>
      <h2>
        <i class="icon iconfont">&#xe612;</i>
        <span>{{ $t("patch.cve_list") }}</span>
      </h2>
    </template>
    <template v-slot:content>
      <el-dialog
        :title="title"
        :visible.sync="display"
        :before-close="handleClose"
      >
        <delete
          v-if="type === 'delete'"
          :content="delete_content"
          :handleCancel="handleClose"
          :handleOk="handleDeleteOk"
        >
        </delete>
        <cve-table
          v-else-if="type === 'create'"
          @click="handleClose"
          :id="$route.params.id"
        ></cve-table>
      </el-dialog>
      <p class="kylin-page-label">
        {{ $t("patch.cve_group_cve_list_information") }}
      </p>
      <ky-table
        :getData="getGroupCveList"
        :showSelect="true"
        :search_data="search_data"
        :search_criteria="search_criteria"
        ref="table"
      >
        <template v-slot:table-action>
          <authorization-button
            class="kylin-item-button"
            @click="handleRepair"
            :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
          >
            {{ $t("patch.one_click_repair") }}
          </authorization-button>
          <authorization-button class="kylin-item-button" @click="handleAdd">
            {{ $t("patch.add_cve") }}
          </authorization-button>
          <authorization-button
            class="kylin-delete"
            @click="handleDelete"
            name="config:delete"
            :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
          >
            {{ $t("delete") }}
          </authorization-button>
        </template>
        <template v-slot:table>
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline>
                <el-form-item :label="$t('description')">
                  <span>{{ props.row.description }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="cveId" :label="$t('patch.CVE_number')">
          </el-table-column>
          <el-table-column prop="sysNum" :label="$t('patch.influence_system')">
          </el-table-column>
          <el-table-column prop="severity" :label="$t('patch.risk_level')">
          </el-table-column>
          <el-table-column prop="status" :label="$t('system.status')">
          </el-table-column>
          <el-table-column prop="issued_date" :label="$t('patch.publish_time')">
          </el-table-column>
        </template>
      </ky-table>
    </template>
  </page>
</template>

<script>
import Page from "@/components/Page";
import kyTable from "@/components/KyTable";
import { getGroupCveList, deleteGroupCve, issueByCve } from "@/api/cve_group";
import Delete from "@/components/Delete";
import CveTable from "./CveTable.vue";
import AuthorizationButton from "@/components/AuthorizationButton";
export default {
  components: {
    Page,
    kyTable,
    AuthorizationButton,
    Delete,
    CveTable,
  },
  data() {
    return {
      showSelect: true,
      title: "",
      display: false,
      edit_id: null,
      delete_content: "",
      type: "",
      search_criteria: {
        sortBy: "id",
        sortOrder: "desc",
        id: this.$route.params.id,
      },
      search_data: {
        cveId: {
          type: "text",
          name: "patch.CVE_number",
        },
        severity: {
          type: "select",
          name: "patch.risk_level",
          items: [
            { key: "Low", name: "低风险" },
            { key: "Moderate", name: "中风险" },
            { key: "Important", name: "高风险" },
            { key: "Critical", name: "严重风险" },
          ],
        },
        issued_date: {
          type: "date",
          name: "patch.publish_time",
        },
      },
    };
  },
  methods: {
    getGroupCveList,
    handleEdit(row) {
      this.display = true;
      this.title = this.$t("configure.edit_configure_item");
      this.edit_id = row.id;
      this.type = "update";
    },
    handleClose(type) {
      this.display = false;
      this.edit_id = null;
      (this.title = ""), (this.delete_content = "");
      this.type = "";
      if (type === "success") {
        this.$refs.table.handleSearch();
      }
    },
    handleDelete() {
      this.display = true;
      this.title = this.$t("configure.delete_configure");
      this.type = "delete";
      let arr = [];
      this.$refs.table.selectRow.rows.map(function (row) {
        arr.push(row.cveId);
      });
      this.delete_content = this.$t(
        "configure.delete_configure_content"
      ).replace("%s", arr.toString());
    },
    handleAdd() {
      this.display = true;
      this.title = this.$t("patch.add_cve");
      this.type = "create";
    },
    async handleRepair() {
      let data = [];
      if (this.$refs.table.select_id === "2") {
        const res = await getGroupCveList({
          ...this.$route.query,
          paged: false,
          id: this.$route.params.id,
        });
        res.data.data.results.map((item) => {
          if (!this.$refs.table.selectRow.excludeIds.includes(item.id)) {
              data.push(item.id);
          }
        });
      } else {
        this.$refs.table.selectRow.rows.forEach((item) => {
          data.push(item.id);
        });
      }
      const res = await issueByCve(data);
      this.$router.push({
        name: "log_detail",
        params: { id: res.data.data.id },
      });
      if (res.data.code == "0") {
        this.$message({
          type: "success",
          message: res.data.message,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.message,
        });
      }
    },
    async handleDeleteOk() {
      let data = [];
      if (this.$refs.table.select_id === "2") {
        const res = await getGroupCveList({
          ...this.$route.query,
          paged: false,
          id: this.$route.params.id,
        });
        res.data.data.results.map((item) => {
          if (!this.$refs.table.selectRow.excludeIds.includes(item.id)) {
              data.push(item.id);
          }
        });
      } else {
        this.$refs.table.selectRow.rows.forEach((item) => {
          data.push(item.id);
        });
      }
      const res = await deleteGroupCve(data);
      if (res.data.code == "0") {
        this.$message({
          type: "success",
          message: res.data.message,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.message,
        });
      }
      this.display = false;
      this.edit_id = null;
      this.type = "";
      this.$refs.table.refresh();
    },
  },
};
</script>

<style>
</style>